<?php

namespace App\Library\Rms;

use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Support\Facades\Cache;

class RmsUserApi extends RmsBaseApi
{
    // 用户状态
    const USER_STATUS_OK = 0;
    const USER_STATUS_ERROR = 1;

    // APP用户默认头像
    const USER_DEFAULT_AVATAR_APP = 'https://file.51kuafu.com/dc/1a/ed20828b5fd6b855662c46f02278f54ec8552953f0a7c37df1ee94777d9f.png';

    /**
     * 通过手机号批量查询用户信息
     * @param array $mobile
     * @return array
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function getUserInfoByMobiles(array $mobiles)
    {
        $params = [
            'mobiles' => implode(',', $mobiles),
        ];

        return $this->rmsCurl->do('api/internal/user/getUserInfoByMobiles', $params, 'GET');
    }

    /**
     * 创建用户
     * @param string $mobile
     * @param string $name
     * @return array
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function createUser(string $mobile, string $name, array $brandRoles = [])
    {
        $params = [
            'mobile' => $mobile,
            'name'   => $name,
            'roles'  => [],         // roles是全量覆盖
        ];

        if ($brandRoles) {
            $params['roles']['brandRoles'] = $brandRoles;
        }

        return $this->rmsCurl->setHeader(['brandId' => 1])->do('api/internal/user/create', $params, 'POST');
    }

    public function setUserFeishuIdNByteUserId(int $userId, array $data)
    {
        $params = [
            'userId' => $userId,
        ];

        return $this->rmsCurl->setHeader(['brandId' => 1])->do('api/internal/user/setUserInfoById', array_merge($params, $data), 'POST');
    }

    /**
     * 通过username批量获取用户信息
     * @param array $usernames
     * @return array
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function getInfoByUsernames(array $usernames, bool $cached = false)
    {
        $params = [
            'usernames' => $usernames,
        ];

        if (!$cached) {
            return $this->rmsCurl->setHeader(['brandId' => 1])->do('api/internal/user/getInfoByUsernames', $params, 'GET');
        }

        return Cache::remember('getInfoByUsernames', 300, function () use ($params) {
            return $this->rmsCurl->setHeader(['brandId' => 1])->do('api/internal/user/getInfoByUsernames', $params, 'GET');
        });
    }

    /**
     * 查询用户的权限列表
     * @param int $userId
     * @param int $shopId
     * @return array
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function getUserPermission(int $userId, int $shopId = 0)
    {
        $params = [
            'userId' => $userId,
        ];

        return $this->rmsCurl->setHeader(['brandId' => 1, 'shopId' => $shopId])->do('api/internal/user/getUserPermission', $params, 'GET');
    }

    /**
     * 查询用户作为店长，关联的所有shopIds
     * @param int $userId
     * @return array
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function getShopIdsByUserManager(int $userId)
    {

        $params = [
            'userId' => $userId,
        ];

        return $this->rmsCurl->setHeader(['brandId' => 1])->do('api/internal/user/getShopIdsByUserManager', $params, 'GET');
    }

    /**
     * 获取飞书用户信息
     * @param string $departmentName
     * @param array $fields
     * @return array
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function getFsUsers(
        string $departmentName,
        array $fields = ['*'],
        string $rootDepartmentId = '',
        string $userIds = ''
    ) {
        $params = [
            'departmentName' => $departmentName,
            'rootDepartmentId' => $rootDepartmentId,
            'fields' => $fields,
            'userIds' => $userIds
        ];

        return $this->rmsCurl->do('api/internal/fsUser/getUserLists', $params, 'GET');
    }

    /**
     * 职位配置
     * @return array
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function roleIdsConfig()
    {
        return $this->rmsCurl->do('api/internal/user/roleIdsConfig', [], 'GET');
    }

    public function getUserInfoByIds(array $userIds, array $fields = ['*'])
    {
        $params = [
            'userIds' => implode(',', $userIds),
            'fields' => $fields,
        ];

        return $this->rmsCurl->do('api/internal/user/getUserInfoByIds', $params, 'GET');

    }

    /**
     * @param string $roles
     * @return array
     * @throws GuzzleException
     */
    public function getUserListByPower(string $roles): array
    {
        $params = [
            'roles' => $roles,
        ];
        return $this->rmsCurl->do('api/internal/user/getUserListByPower', $params, 'GET');
    }
}
